package com.hrms.service.impl;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.dao.*;
import com.hrms.pojo.BasePage;
import com.hrms.pojo.FileManage;
import com.hrms.service.FileManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 员工档案管理 服务实现类
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class FileManageServiceImpl extends BasePage implements FileManageService {
    //注入mapper
    @Autowired(required = false)
    FileManageMapper fileManageMapper;
    @Autowired(required = false)
    ContractManageMapper contractManageMapper;
    @Autowired(required = false)
    EmpInformationMapper empInformationMapper;
    @Autowired(required = false)
    PersonnelTransferMapper personnelTransferMapper;
    @Autowired(required = false)
    AttenManageMapper attenManageMapper;
    @Autowired(required = false)
    SalaryInfoMapper salaryInfoMapper;

    @Override
    public HashMap<String, Object> add(FileManage fileManage) {
        HashMap<String,Object> map=new HashMap<>();
        //新增
        int num=fileManageMapper.insert(fileManage);
        if (num>0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> select(FileManage fileManage) {
        HashMap<String,Object> map=new HashMap<>();
        //定义分页对象
        Page<FileManage> page=new Page<>(fileManage.getPage(),fileManage.getRow());
        //条件构造器
        QueryWrapper<FileManage> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq(fileManage.getStaffId()!=null,"staff_id",fileManage.getStaffId());
        queryWrapper.like(fileManage.getArchivesName()!=null&&!fileManage.getArchivesName().equals(""),"archives_name",fileManage.getArchivesName());

        //查询数据库
        Page<FileManage> aList= fileManageMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        map.put("curPage",fileManage.getPage());
        if(fileManage.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",fileManage.getPage()-1);

        }
        if(fileManage.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());

        }else{
            map.put("nextPage",fileManage.getPage()+1);

        }
        //每页显示条数
        map.put("row",fileManage.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> update(FileManage fileManage) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //修改
        int num = fileManageMapper.updateById(fileManage);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num1 = fileManageMapper.deleteByStaffId(a);
        int num2 = contractManageMapper.deleteByStaffId(a);
        int num3 = personnelTransferMapper.deleteByStaffId(a);
        int num4 = empInformationMapper.deleteByStaffId(a);
        int num5 = attenManageMapper.deleteByStaffId(a);
        int num6 = salaryInfoMapper.deleteByStaffId(a);
        if((num1>0)||(num2>0)||(num3>0)||(num4>0)||(num5>0)||(num6>0)){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }


    @Override
    public void excelExport(HttpServletResponse response) throws IOException {
        List<FileManage> list = fileManageMapper.selUserMapper();
        String fileName = "档案名单";
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xls");
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS,true);
        Sheet sheet = new Sheet(1,0,FileManage.class);
        //设置自适应宽度
        sheet.setAutoWidth(Boolean.TRUE);
        sheet.setSheetName("档案名单");
        writer.write(list,sheet);
        writer.finish();
        out.flush();
        response.getOutputStream().close();
        out.close();
    }
}
